//
//  InitHoldViewController.swift
//  weile
//
//  Created by LKM on 16/9/27.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit
import SnapKit

/**
 直到加载完数据才能进入。如果加载失败就谈对话框。自动登录什么的都在这
 */
class InitHoldViewController: BaseViewController {

    @IBOutlet weak var contaiView: UIView!
    @IBOutlet weak var img_logo: UIImageView!
    var ai_loading: UIActivityIndicatorView!
    var lbl_loading: UILabel!
    
    var luanchTime:Int64!
    
    override func viewDidLoad() {

        // Do any additional setup after loading the view.
        luanchTime=ServerTimeSync.currentTimeMillis();
        
        initView();
        
        loadData();//先放这里，其实加载数据不应该放这边的拉
        
        super.viewDidLoad()
//        LKMLog("\(StringHelp.substring(str: "123456789", sIndex: 2, length:11))")//34
    }
    
    var taskAlls = [Int]();
    
    var isLoginEnd = false;
    var isLoginSuccess = false;
    //开始加载数据
    private func loadData(){
        Api.login(isRetry: true) { (re:ResponEntity) in
            ViewHelp.hideProgressActivity(containView: self.view)
            
            self.isLoginEnd=true;
            self.isLoginSuccess = re.isSuccess();
            
            self.lbl_loading.isHidden=true;
            self.ai_loading.isHidden=true;
            
            //保证启动页 + 显示这个的时间超过2s
            let tc = 4000 - (ServerTimeSync.currentTimeMillis() - self.luanchTime)
            if( tc > 0 ){
                //   还没到时间
                Handler.postDelayed( Float(tc/1000), closure: { [weak self] in
                    if self == nil{
                        return;
                    }
                    self?.goInt();
                })
                
            }else{
                self.goInt();
            }
        }
        
        taskAlls.append(Api.LoginTaskSerialNum);
    }
    
    var isOnceGoInt=false;
    func goInt(){
        if(!isLoginEnd){
            return
        }
        if(isOnceGoInt){
            return
        }
        
        isOnceGoInt=true
        
        if(isLoginSuccess){
            ActivityRequest.goMain()
        }else{
            ActivityRequest.goLogin()
        }
    }
    
    deinit {
        Api.destroyTask(taskSerialNumbers: taskAlls);
    }
    
    private func initView(){
        //在启动页面添加加载框
    
        let label: UILabel = UILabel()
        label.text = "初始化数据中..."
        label.font=UIFont.systemFont(ofSize: 16);
        self.view.addSubview(label)
        label.snp.makeConstraints { (maker) in
            maker.centerX.equalTo(view);
            //maker.bottom.equalTo(view.snp_bottom).offset(-20);
            maker.bottom.equalTo(view.snp.bottom).offset(-20)
        }
        self.lbl_loading=label;
        
        
        ai_loading = UIActivityIndicatorView();
        ai_loading.activityIndicatorViewStyle = .gray
        // ai_loading.backgroundColor=#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1);
//        ai_loading.center=CGPoint(x: 30, y: 30)
        view.addSubview(ai_loading);
        ai_loading.startAnimating();
        
        ai_loading.snp.makeConstraints { (maker:ConstraintMaker) in
            maker.centerX.equalTo(view);
            maker.size.equalTo(CGSize(width: 30, height: 30));
            //maker.bottom.equalTo(view.snp_bottom).offset(-20);
            maker.bottom.equalTo(label.snp.top).offset(-10)
        }

    }
    @IBAction func onFastGoIntClick(_ sender: AnyObject) {
        goInt();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
